package com.tianpengtech.admin.task;
import java.util.List;
import java.util.Map;


import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.exception.ServiceException;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.im.kit.PushKit;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.opertion.OperationDao;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.NotEqualCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.util.SendSmsMsg;
import com.tianpengtech.common.util.XxxUtil;
import com.tianpengtech.common.util.push.Jpush;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterThan;
import com.tianpengtech.common.validator.rules.string.StringLengthBetween;

public class TaskAdminServiceImpl extends AbstractAdmin implements IAdminOpt{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model<?> model) {

	}
	
	
	@Override
	public boolean deleteById(Model<?> model, int id) throws ValidateException, ServiceException {
		
		model.set(Task.DELETETAG, Task.DELETE_STATUS_YES);
		return model.update();
	}


	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {
		queryConstruct
		.and(new EqualCondition(Task.DELETETAG, Task.DELETE_STATUS_NO))
		.and(new NotEqualCondition(Task.STATUS, Task.STATUS_WAIT));
		String task_title = model.getStr(Task.TASK_TITLE);
		if(StringUtils.isNotEmpty(task_title)){
			queryConstruct
			.and(new LikeCondition(Task.TASK_TITLE,"%"+task_title+"%"));
		}
	}

	@Override
	public String tableName() {
		return Task.dao.tableName();
	}

	@Override
	public void changeStatus(Model<?> model, long id, int status, Kv params) throws ValidateException {
		
		String content = params.getStr(Opertion.CONTENT);
		Validator validator = new Validator();
		if(status==Task.STATUS_FAILURE){
			validator.addRule( new StringLengthBetween(content,"拒绝理由必须是1-40个字符之间",1,40));
		}
		validator.addRule( new NumbericMustBeGreaterThan(id,0,"id值必须要大于0")).excute();
		model.set(Task.ID, id);
		
		Task task = (Task)model.findById(id);
		int tempStatus = (task.isStarted())?Task.STATUS_START:Task.STATUS_SUCCESS;
		if(status==Task.STATUS_FAILURE){
			model.set(Task.STATUS,Task.STATUS_FAILURE);
		}else{
			model.set(Task.STATUS,tempStatus);
		}
		model.update();
		Model<Task> temp = Task.dao.findById(id);
		
		Account account = XxxUtil.readSessionInfo();
		long userId = account.getLong(Account.ID);
		if(status==Task.STATUS_SUCCESS){
			String result = "【通过】《"+temp.getStr(Task.TASK_TITLE)+"》的审核请求";
			PushKit.pushSystemMessageNow("管理员"+result);
			OperationDao.ME.recordLog(id, Opertion.OPT_TASK_LOG,userId,result);
			Jpush.pushCustomContentToAll("任务《"+temp.getStr(Task.TASK_TITLE)+"》发布啦，快来接任务吧！",Jpush.PUSH_TASK);
			Jpush.pushMessageInfoToAll("任务《"+temp.getStr(Task.TASK_TITLE)+"》发布啦，快来接任务吧！",Jpush.PUSH_TASK,id+"");
			
			if(SysConfigDao.ME.isSendTaskSMS()){
				List<Fair> fairs = readAllFair();
				SendTaskSMSThread thread = new SendTaskSMSThread(fairs);
				thread.start();
			}
		}
		if(status==Task.STATUS_FAILURE){
			String result = "【拒绝】《"+temp.getStr(Task.TASK_TITLE)+"》的审核请求,原因是:"+content;
			PushKit.pushSystemMessageNow("管理员"+result);
			OperationDao.ME.recordLog(id, Opertion.OPT_TASK_LOG,userId,result);
		}
	}
	
	
	private List<Fair> readAllFair(){
		String sql="select * from "+Fair.dao.tableName()+" where status=? and deletetag=?";//找出所有认证通过的网红，发送数据信息
		return Fair.dao.find(sql,Fair.STATUS_SUCCESS,Fair.DELETE_STATUS_NO);
	}
	
	
	public static void main(String[] args) {
		SendSmsMsg.sendNewTaskInfo("13641489216", SendSmsMsg.TEMPLATE_NEW_MISSION);
	}

}